package main

import (
	"context"
	"fmt"
	"github.com/go-redis/redis/v8"
	"strings"
)

func main() {
	rdb := redis.NewClient(&redis.Options{
		Addr:     "192.168.3.132:7001",
		Password: "", // no password set
		DB:       0,  // use default DB
	})
	ctx := context.Background()
	nodes := rdb.ClusterNodes(ctx).Val()
	nodeArray := strings.Split(nodes, "\n")
	masters := make([]string, 0)
	for i := range nodeArray {
		node := nodeArray[i]
		if node == "" {
			continue
		}
		ss := strings.Split(node, " ")
		if strings.Contains(ss[2], "master") {
			masters = append(masters, strings.Split(ss[1], "@")[0])
		}
	}
	for i := range masters {
		master := masters[i]
		rdb = redis.NewClient(&redis.Options{
			Addr:     master,
			Password: "", // no password set
			DB:       0,  // use default DB
		})
		rdb.FlushAll(ctx)
		fmt.Println(master, "flush完毕")
	}
}
